package cn.iocoder.yudao.module.trade.controller.admin.depositorderreturnlog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.depositorderreturnlog.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositorderreturnlog.DepositOrderReturnLogDO;
import cn.iocoder.yudao.module.trade.service.depositorderreturnlog.DepositOrderReturnLogService;

@Tag(name = "管理后台 - 退瓶记录")
@RestController
@RequestMapping("/trade/deposit-order-return-log")
@Validated
public class DepositOrderReturnLogController {

    @Resource
    private DepositOrderReturnLogService depositOrderReturnLogService;

    @PostMapping("/create")
    @Operation(summary = "创建退瓶记录")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return-log:create')")
    public CommonResult<Long> createDepositOrderReturnLog(@Valid @RequestBody DepositOrderReturnLogSaveReqVO createReqVO) {
        return success(depositOrderReturnLogService.createDepositOrderReturnLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新退瓶记录")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return-log:update')")
    public CommonResult<Boolean> updateDepositOrderReturnLog(@Valid @RequestBody DepositOrderReturnLogSaveReqVO updateReqVO) {
        depositOrderReturnLogService.updateDepositOrderReturnLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除退瓶记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return-log:delete')")
    public CommonResult<Boolean> deleteDepositOrderReturnLog(@RequestParam("id") Long id) {
        depositOrderReturnLogService.deleteDepositOrderReturnLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得退瓶记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return-log:query')")
    public CommonResult<DepositOrderReturnLogRespVO> getDepositOrderReturnLog(@RequestParam("id") Long id) {
        DepositOrderReturnLogDO depositOrderReturnLog = depositOrderReturnLogService.getDepositOrderReturnLog(id);
        return success(BeanUtils.toBean(depositOrderReturnLog, DepositOrderReturnLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得退瓶记录分页")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return-log:query')")
    public CommonResult<PageResult<DepositOrderReturnLogRespVO>> getDepositOrderReturnLogPage(@Valid DepositOrderReturnLogPageReqVO pageReqVO) {
        PageResult<DepositOrderReturnLogDO> pageResult = depositOrderReturnLogService.getDepositOrderReturnLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepositOrderReturnLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退瓶记录 Excel")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return-log:export')")
    @OperateLog(type = EXPORT)
    public void exportDepositOrderReturnLogExcel(@Valid DepositOrderReturnLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepositOrderReturnLogDO> list = depositOrderReturnLogService.getDepositOrderReturnLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "退瓶记录.xls", "数据", DepositOrderReturnLogRespVO.class,
                        BeanUtils.toBean(list, DepositOrderReturnLogRespVO.class));
    }

}